﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNetSpeech;
using System.Windows.Forms;

namespace xml
{
    public class Voice
    {
        
        SpVoice voice = new SpVoice();
        
        
        public void Read(string text)
        {
            voice.Volume = 100;
            try
            {
                // voice.Voice = voice.GetVoices("name=Microsoft Lily", "").Item(0);
                voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync);


            }
            catch (Exception ex)
            {

              throw;
            }

        }
        public void Pause()
        {
            voice.Pause();
        }
        public void Resume()
        {
            voice.Resume();
        }
        public void Stop()
        {


            voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);


        }
        public void Save(string text)
        {
            
            SpVoice voice = new SpVoice();
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
            dialog.Title = "保存WAV文件";
            dialog.FilterIndex = 2;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
                SpFileStream spFileStream = new SpFileStream();
                spFileStream.Open(dialog.FileName, spFileMode, false);
                voice.AudioOutputStream = spFileStream;
                voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                voice.WaitUntilDone(1000);
                spFileStream.Close();
            }
        }
    }
}
